package com.javandad.app.client;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.javandad.app.client.cells.UserCell;
import com.javandad.app.shared.entity.User;

public class GroupView implements EntryPoint {

	RootPanel memberStatusContainer = RootPanel.get("memberStatusContainer");
	RootPanel membersListContainer = RootPanel.get("membersListContainer");
	NetworkServiceAsync network = GWT.create(NetworkService.class);

	@Override
	public void onModuleLoad() {
		if (memberStatusContainer != null)
			initMemberStatusGUI();
		if (membersListContainer != null)
			initMembersListGUI();
	}

	private String getContainerParameter(RootPanel container, String name) {
		String value = container.getElement().getAttribute(name);
		if (value.isEmpty())
			value = null;
		return value;
	}

	VerticalPanel membersListPanel;
	CellList<User> membersList;

	private void initMembersListGUI() {
		String groupURL = getContainerParameter(membersListContainer, "groupURL");
		membersListPanel = new VerticalPanel();
		membersListContainer.add(membersListPanel);
		membersList = new CellList<User>(new UserCell());
		final SingleSelectionModel<User> selectionModel = new SingleSelectionModel<User>();
		membersList.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
			public void onSelectionChange(SelectionChangeEvent event) {
				User user = selectionModel.getSelectedObject();
				Window.open("home.jsp?"+CodesvrParam.getCodesvrParam()+"&view="+user.getUsername(), "_self", "");
			}
		});
		membersListPanel.add(membersList);
		network.getMembers(groupURL, new AsyncCallback<List<User>>() {

			@Override
			public void onSuccess(List<User> members) {
				membersList.setRowData(members);
			}

			@Override
			public void onFailure(Throwable caught) {
			}
		});
	}

	Label memberStatus;
	Button becomeMember;
	Button leave;

	private void initMemberStatusGUI() {
		final String username = getContainerParameter(memberStatusContainer, "username");
		final String groupURL = getContainerParameter(memberStatusContainer, "groupURL");
		memberStatus = new Label("fetchin'...");
		memberStatusContainer.add(memberStatus);
		if (username == null || username == "") {
			memberStatus.setText("You are not logged in.");
			return;
		}
		network.isMember(username, groupURL, new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Boolean isMember) {
				if (isMember) {
					memberStatus.setText("You are a member.");
					leave = new Button("Leave group");
					memberStatusContainer.add(leave);
					leave.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							network.removeMember(username, groupURL, new AsyncCallback<Boolean>() {

								@Override
								public void onFailure(Throwable caught) {
									// Frankly, I don't care.
								}

								@Override
								public void onSuccess(Boolean result) {
									Window.open("group.jsp?"+CodesvrParam.getCodesvrParam()+"&group="+groupURL, "_self", "");
								}});
						}
					});
				} else {
					memberStatus.setText("You are not a member.");
					becomeMember = new Button("Become a member");
					memberStatusContainer.add(becomeMember);
					becomeMember.addClickHandler(new ClickHandler() {

						@Override
						public void onClick(ClickEvent event) {
							network.addMember(username, groupURL, new AsyncCallback<Boolean>() {

								@Override
								public void onFailure(Throwable caught) {
								}

								@Override
								public void onSuccess(Boolean result) {
									if (result) {
										Window.alert("Congratulations for joining " + groupURL);
										Window.open("group.jsp?"+CodesvrParam.getCodesvrParam()+"&group="+groupURL, "_self", "");
									}
									else
									{
										Window.alert("Having trouble joining " + groupURL + "...");
									}
								}
							});
						}
					});
				}
			}
		});
	}

}
